package com.meiyu.live.ui.mine;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.param.GetcodeParams;
import com.meiyu.live.net.param.RegParams;
import com.meiyu.live.net.response.BaseResponse;
import com.meiyu.live.util.ToastUtil;
import com.meiyu.live.widget.NetLoadingDailog;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by zbl on 2016/8/29.
 */
public class bindPhoneActivity extends BaseActivity implements View.OnClickListener {

    protected NetLoadingDailog dialog_loading;
    private View btn_back;
    private TextView btn_register, btn_sendcode;
    private EditText et_account, et_code;//,et_nickname;

    private static final int SEND_CODE_AFTER_TIME = 60;
    private boolean isGetCode = false;
    private int afterTime = SEND_CODE_AFTER_TIME;
    private Timer timer;
    private Handler codeTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1: {
                    if (afterTime == 0) {
                        timer.cancel();
                        timer = null;
                        afterTime = SEND_CODE_AFTER_TIME;
                        btn_sendcode.setText("发送验证码");
                        btn_sendcode.setTextColor(0xffffffff);
                        btn_sendcode.setBackgroundResource(R.drawable.background_btn_startlive);
                        btn_sendcode.setEnabled(true);
                        isGetCode = false;
                    } else {
                        afterTime--;
                        btn_sendcode.setText(afterTime + "s后重发");
                        btn_sendcode.setTextColor(0xffededed);
                        btn_sendcode.setBackgroundResource(R.drawable.background_edit);
                        btn_sendcode.setEnabled(false);
                    }
                }
                break;
            }
        }
    };

    private TimerTask timerTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bindphone);
        init();
    }

    private void init() {
        dialog_loading = new NetLoadingDailog(this);
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        btn_register = (TextView) findViewById(R.id.btn_register);
        btn_register.setOnClickListener(this);
        btn_register.setEnabled(false);
        btn_sendcode = (TextView) findViewById(R.id.btn_sendcode);
        btn_sendcode.setOnClickListener(this);
        btn_sendcode.setEnabled(false);

        et_account = (EditText) findViewById(R.id.et_account);
//        et_nickname = (EditText) findViewById(R.id.et_nickname);
        et_code = (EditText) findViewById(R.id.et_code);

        et_account.addTextChangedListener(textWatcher);
//        et_nickname.addTextChangedListener(textWatcher);
        et_code.addTextChangedListener(textWatcher);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (et_account.getText().length() > 0
                    && et_code.getText().length() > 0) {
//                btn_register.setTextColor(0xff593512);
                btn_register.setBackgroundResource(R.drawable.background_btn_startlive);
                btn_register.setEnabled(true);
            } else {
//                btn_register.setTextColor(0xffffffff);
                btn_register.setBackgroundResource(R.drawable.background_login_disable);
                btn_register.setEnabled(false);
            }

            if (!isGetCode) {
                if (et_account.getText().length() > 0) {
                    btn_sendcode.setTextColor(0xffffffff);
                    btn_sendcode.setBackgroundResource(R.drawable.background_btn_startlive);
                    btn_sendcode.setEnabled(true);
                } else {
                    btn_sendcode.setTextColor(0xffededed);
                    btn_sendcode.setBackgroundResource(R.drawable.background_edit);
                    btn_sendcode.setEnabled(false);
                }
            }
        }
    };

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        } else if (view == btn_sendcode) {
            getCode(et_account.getText().toString().trim());
        } else if (view == btn_register) {
            getData(
                    "",//et_nickname.getText().toString().trim(),
                    et_account.getText().toString().trim(),
                    et_code.getText().toString().trim());
        }
    }

    /**
     * 获取验证码
     */
    private void getCode(String account) {
        if (isGetCode) {
            return;
        }
        if (TextUtils.isEmpty(account)) {
            ToastUtil.show("请输入手机号");
            return;
        }
        long accountNumber = 0;
        try {
            accountNumber = Long.parseLong(account);
        } catch (NumberFormatException e) {
            ToastUtil.show("请输入正确的手机号");
            return;
        }
        isGetCode = true;
        request(new GetcodeParams(accountNumber, "mobile"), BaseResponse.class, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse onlyStatusResponse) {
                if (onlyStatusResponse.getStatusCode() == Constant.StatusCode_Success) {
                    if (timer != null) {
                        timer.cancel();
                    }
                    timer = new Timer(true);
                    timerTask = new TimerTask() {
                        public void run() {
                            Message message = codeTaskHandler.obtainMessage(1);
                            codeTaskHandler.sendMessage(message);
                        }
                    };
                    timer.schedule(timerTask, 0, 1000);
                } else {
                    ToastUtil.show("获取验证码失败");
                }
            }
        });
    }

    private void getData(final String nickname, final String account, final String code) {
        if (TextUtils.isEmpty(account)) {
            ToastUtil.show("请输入手机号");
            return;
        } else if (TextUtils.isEmpty(code)) {
            ToastUtil.show("请输入验证码");
            return;
        }
//        else if (TextUtils.isEmpty(nickname)) {
//            ToastUtil.show("请输入昵称");
//            return;
//        }
        long accountNumber = 0;
        try {
            accountNumber = Long.parseLong(account);
        } catch (NumberFormatException e) {
            ToastUtil.show("请输入正确的手机号");
            return;
        }
        dialog_loading.loading();
        RegParams regParams = new RegParams(accountNumber, code);
        regParams.setUrl(Constant.UrlParams.MOBILE_AUTH);
        request(regParams, BaseResponse.class, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse onlyStatusResponse) {
                dialog_loading.dismissDialog();
                if (onlyStatusResponse != null && onlyStatusResponse.getStatusCode() == Constant.StatusCode_Success) {
                    ToastUtil.show("绑定成功");
                    setResult(RESULT_OK);
                    finish();
                } else {
                    ToastUtil.show(onlyStatusResponse.getMsg());
                }
            }
        });
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onError() {
        super.onError();
        dialog_loading.dismissDialog();
        isGetCode = false;
    }
}
